package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.Voice;
import com.tanhua.dubbo.api.VoiceApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Random;

@Service
public class VoiceApiImpl implements VoiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Voice voice) {
        mongoTemplate.save(voice);
    }

    @Override
    public Voice findOne(Long userId) {
        Query query = new Query(Criteria.where("userId").ne(userId));
        List<Voice> voices = mongoTemplate.find(query, Voice.class);
        Random random = new Random();
        int i = random.nextInt(voices.size());

        return voices.get(i);
    }
}
